package MAD.examples;



import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;


public class DiceActivity extends Activity {

	Button btnRole, btnClear;//this holds the button we defined in our xml.
	RadioButton rbtnOne, rbtnTwo;
	RadioGroup group; 
	RadioButton temp;
	ImageView imgOne, imgTwo;
	final Random random = new Random();

	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		/**
		 * We first define the element we will be using 
		 */
		btnRole = (Button) findViewById(R.id.button1);	// the role button
		btnClear = (Button) findViewById(R.id.button2);	// the clear button
		imgOne = (ImageView) findViewById(R.id.imageView1);	// 1st ImageView 
		imgTwo = (ImageView) findViewById(R.id.imageView2);	// 2nd ImageView
		group = (RadioGroup) findViewById(R.id.radioGroup1); // the Radio Button Group


		// creating a button listener for the role button
		btnRole.setOnClickListener(new View.OnClickListener(){
			/*************************************
			 * this is the method that takes care
			 *of what happens when the button is 
			 *clicked. It has to be implemented
			 *or else there will be errors
			 **********************************/
			public void onClick(View v) {
	
				int id = group.getCheckedRadioButtonId(); // getting the "id" of the RadioButton in the Radio group that was selected

				// This is like a null check, if no RadioButton is checked getCheckedRadioButtonId will return -1
				if (id != -1 ) { // if getCheckedRadioButtonId was not -1

					String radioText;	// used to store the text from the selected RadioButton

					temp =(RadioButton) findViewById(id); // temp will point to the selected RadioButton

					radioText = (String) temp.getText();	//get the text from the selected RadioButton
					
					if (radioText.equals("1")){
						imgTwo.setImageResource(R.drawable.blankbig);
						setDie(imgOne, random.nextInt(6));
					}else {
						setDie(imgOne, random.nextInt(6));
						setDie(imgTwo, random.nextInt(6));
					}
				}
			}
		});

		// Button listener for clear
		btnClear.setOnClickListener(new View.OnClickListener(){	
			public void onClick(View v) {
				imgOne.setImageResource(R.drawable.blankbig); 
				imgTwo.setImageResource(R.drawable.blankbig);
			}
		});



	}


	private void setDie (ImageView view, int random){
		switch (random){
		case 0 : view.setImageResource(R.drawable.one);
		break;
		case 1 :view.setImageResource(R.drawable.two);
		break;
		case 2 :view.setImageResource(R.drawable.three);
		break;
		case 3 :view.setImageResource(R.drawable.four);
		break;
		case 4 :view.setImageResource(R.drawable.five);
		break;
		case 5 :view.setImageResource(R.drawable.six);
		break;
		}

	}

}
